package cn.funnymap.lgis.tools.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jiao xn
 */
public class UrlCoderUtil {
    static BitSet bitSet;

    private UrlCoderUtil() {
    }

    static {
        bitSet = new BitSet(128);
        int i;
        for (i = 'a'; i <= 'z'; i++) {
            bitSet.set(i);
        }
        for (i = 'A'; i <= 'Z'; i++) {
            bitSet.set(i);
        }
        for (i = '0'; i <= '9'; i++) {
            bitSet.set(i);
        }
        bitSet.set('+');
        bitSet.set('-');
        bitSet.set('_');
        bitSet.set('.');
        bitSet.set('*');
        bitSet.set(':');
        bitSet.set('/');
    }

    /**
     * 字符串是否经过了url encode
     *
     * @param text 字符串
     * @return true表示是
     */
    public static boolean hasEnCode(String text) {
        if (StringUtils.isEmpty(text)) {
            return false;
        }

        for (int i = 0; i < text.length(); i++) {
            int c = text.charAt(i);
            if (!bitSet.get(c)) {
                return false;
            }
            if (c == '%' && (i + 2) < text.length()) {
                // 判断是否符合urlEncode规范
                char c1 = text.charAt(i + 1);
                char c2 = text.charAt(i + 2);

                if (isDigit16Char(c1) || isDigit16Char(c2)) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 对字符串s用charset方式编码
     *
     * @param s 需要编码的字符串
     * @return 编码后的字符串
     */
    public static String encode(String s) throws UnsupportedEncodingException {
        return encode(s, StandardCharsets.UTF_8);
    }

    /**
     * 对字符串s用charset方式编码
     *
     * @param s       需要编码的字符串
     * @param charset 编码类型
     * @return 编码后的字符串
     */
    public static String encode(String s, Charset charset) throws UnsupportedEncodingException {
        if (StringUtils.isBlank(s)) {
            return null;
        }

        return URLEncoder.encode(s, charset.name());
    }

    /**
     * 对已编码的字符串解码
     *
     * @param s 字符串
     * @return 解码后字符串
     */
    public static String decode(String s) throws UnsupportedEncodingException {
        return decode(s, StandardCharsets.UTF_8);
    }

    /**
     * 对已编码的字符串解码
     *
     * @param s       字符串
     * @param charset 解码方式
     * @return 解码后字符串
     */
    public static String decode(String s, Charset charset) throws UnsupportedEncodingException {
        return URLDecoder.decode(s, charset.name());
    }

    public static String encodeChinese(String url) throws UnsupportedEncodingException {
        Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
        String tmp;
        while (matcher.find()) {
            tmp = matcher.group();
            url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
        }
        return url.replace(" ","%20");
    }

    /**
     * 判断c是否是16进制的字符
     *
     * @param c 字符
     * @return true表示是
     */
    private static boolean isDigit16Char(char c) {
        return (c < '0' || c > '9') && (c < 'A' || c > 'F');
    }
}
